package com.hjm.project.accounts.virtualPolicy.controller;

import java.io.File;
import java.util.List;
import java.util.concurrent.CompletableFuture;

import com.hjm.project.job.task.domain.JobTask;
import com.hjm.project.job.task.service.IJobTaskService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import com.hjm.framework.aspectj.lang.annotation.Log;
import com.hjm.framework.aspectj.lang.enums.BusinessType;
import com.hjm.project.accounts.virtualPolicy.domain.VirtualPolicy;
import com.hjm.project.accounts.virtualPolicy.service.IVirtualPolicyService;
import com.hjm.framework.web.controller.BaseController;
import com.hjm.framework.web.domain.AjaxResult;
import com.hjm.common.utils.poi.ExcelUtil;
import javax.annotation.Resource;
import com.hjm.framework.web.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 商户虚户政策Controller
 * 
 * @author maker-platform
 * @date 2022-03-14
 */
@Controller
@RequestMapping("/accounts/virtualPolicy")
public class VirtualPolicyController extends BaseController {
    private String prefix = "accounts/virtualPolicy";

    @Resource
    private IVirtualPolicyService virtualPolicyService;

    @Resource
    private IJobTaskService jobTaskService;

    @RequiresPermissions("accounts:virtualPolicy:view")
    @GetMapping()
    public String virtualPolicy() {
        return prefix + "/virtualPolicy";
    }

    /**
     * 商户提现导入文件下载队列
     * @Param []
     * @Return java.lang.String
     */
    @RequiresPermissions("accounts:virtualPolicy:view")
    @GetMapping("/merJobTask")
    public String merJobTask() {
        return "accounts/merJobTask/merTask";
    }

    /**
     * 查询商户虚户政策列表
     */
    @RequiresPermissions("accounts:virtualPolicy:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(VirtualPolicy virtualPolicy) {
        startPage();
        List<VirtualPolicy> list = virtualPolicyService.selectVirtualPolicyList(virtualPolicy);
        return getDataTable(list);
    }

    /**
     * 导出商户虚户政策列表
     */
    @RequiresPermissions("accounts:virtualPolicy:export")
    @Log(title = "商户虚户政策", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(VirtualPolicy virtualPolicy) {
        List<VirtualPolicy> list = virtualPolicyService.selectVirtualPolicyList(virtualPolicy);
        ExcelUtil<VirtualPolicy> util = new ExcelUtil<VirtualPolicy>(VirtualPolicy.class);
        return util.exportExcel(list, "virtualPolicy");
    }

    /**
     * 新增商户虚户政策
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存商户虚户政策
     */
    @RequiresPermissions("accounts:virtualPolicy:add")
    @Log(title = "商户虚户政策", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(VirtualPolicy virtualPolicy,@RequestParam("file") MultipartFile file) throws Exception {
        //导入文件
        File files = File.createTempFile("prefix", "_" + file.getOriginalFilename());
        file.transferTo(files);
        virtualPolicy.setFileName(file.getOriginalFilename());
        CompletableFuture<JobTask> cbf = virtualPolicyService.insertVirtualPolicy(virtualPolicy, files);
        jobTaskService.asyncUploadFileFuture(cbf);
        return success("导入处理中,请前往下载队列查询结果");
    }

    /**
     * 修改商户虚户政策
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Integer id, ModelMap mmap) {
        VirtualPolicy virtualPolicy = virtualPolicyService.selectVirtualPolicyById(id);
        mmap.put("virtualPolicy", virtualPolicy);
        return prefix + "/edit";
    }

    /**
     * 修改保存商户虚户政策
     */
    @RequiresPermissions("accounts:virtualPolicy:edit")
    @Log(title = "商户虚户政策", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(VirtualPolicy virtualPolicy) {
        return toAjax(virtualPolicyService.updateVirtualPolicy(virtualPolicy));
    }

    /**
     * 删除商户虚户政策
     */
    @RequiresPermissions("accounts:virtualPolicy:remove")
    @Log(title = "商户虚户政策", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(virtualPolicyService.deleteVirtualPolicyByIds(ids));
    }
}
